/*******************************************************************************

PooledRegions analysis_villagelevelaccess.do

2018.08.30 (DJ) Created

This do file puts together two sets of village-level market access data sets.
(1) input side: <Kilimanjaro ViltoAV.dta> and <Manyara ViltoAV.dta>
(2) output side: <Kilimanjaro ViltoMaizemarket.dta> and <Manyara ViltoMaizemarket.dta>

And then it runs a village-level input and output market access regressions.

*******************************************************************************/




/***************************************************************************
***************************************************************************
	PART 1. USING VILLAGE LEVEL INPUT AND OUTPUT ACCESS DATA
****************************************************************************
****************************************************************************/

/*******************************************************************************
	compile independent variables
*******************************************************************************/
 
 #d;
local market_access_varlist "

 has_agrovet_village
 has_av_fertseed_less_10km
 num_av_fertseed_less_10km
	
 vil_to_anyav_km_w1
 
 travelcost_bestadj_av_urea_usd
 km_travel_adj_50kg
 
 min_travel_adj_price_50kg
 min10_travel_adj_price_50kg
 min20_travel_adj_price_50kg
 
 distance_km_nearestmarket 
 max_travel_adj_maize_120kgUSD
 
 num_av_seed_less_10km
 
 unadjusted_price_50kg
 
 has_maizeseller_less_10km
 num_maizeseller_less_10km
 
 unadj_bestprice17_120kgUSD
 cost_travel_120kgUSD
 
 travelcost_DR_adj_av_urea_usd
 km_DR_adj_50kg
 min_DR_adj_price_50kg
 min10_DR_adj_price_50kg
 min20_DR_adj_price_50kg
 unadjusted_price_50kg_DR
 
 
 max_DR_adj_maize_120kgUSD
 DR_unadj_bestprice17_120kgUSD
 DR_cost_travel_120kgUSD
 
 min_DR_adj_price_50kg 
 min_DR3_adj_price_50kg 
 min_DR5_adj_price_50kg 
 min_DR10_adj_price_50kg
 
 unadjusted_price_50kg_DR 
 unadjusted_price_50kg_DR3 
 unadjusted_price_50kg_DR5 
 unadjusted_price_50kg_DR10
 
 travelcost_DR_adj_av_urea_usd 
 travelcost_DR3_adj_av_urea_usd 
 travelcost_DR5_adj_av_urea_usd 
 travelcost_DR10_adj_av_urea_usd
 
 max_DR3_adj_maize_120kgUSD 
 max_DR5_adj_maize_120kgUSD 
 max_DR10_adj_maize_120kgUSD 
 max_DR15_adj_maize_120kgUSD
 
 DR3_unadj_bestprice17_120kgUSD 
 DR5_unadj_bestprice17_120kgUSD 
 DR10_unadj_bestprice17_120kgUSD 
 DR15_unadj_bestprice17_120kgUSD
 
 DR3_cost_travel_120kgUSD 
 DR5_cost_travel_120kgUSD 
 DR10_cost_travel_120kgUSD 
 DR15_cost_travel_120kgUSD
 
 vil_to_anyav_km_r2_w1
 vil_to_anyav_km_AV2_w1
 
 min_NEAR_adj_price_50kg 
 unadjusted_price_50kg_NEAR  
 travelcost_NEAR_adj_av_urea_usd 
 
 max_NEAR_adj_maize_120kgUSD
 NEAR_unadj_bestprice17_120kgUSD  
 NEAR_cost_travel_120kgUSD
 
 has_av_fert_less_10km
 num_av_fert_less_10km
 has_av_seed_less_10km
 num_av_seed_less_10km
 vil_to_anyav_km_r1_seed_w1 
 vil_to_anyav_km_r2_seed_w1
 
 "
 ;


#d cr
	
		
/*******************************************************************************
	append Manyara and Kilimanjaro Data sets
*******************************************************************************/
local indep1 std_google_vil_city_km
local indep2 DH_access_std

local j 0
foreach filename in censusappended_weightedDIST censusappended_DHaccess{
	local ++j
	local indep `indep`j''
	local data ViltoAV_censusappended
	
	// Kilimanjaro
		use "${analysis}/Kilimanjaro ViltoAV_censusappended.dta", clear
		merge 1:1 village_name ward district using "${analysis}/Kilimanjaro ViltoMaizemarket.dta"
		drop _merge
		merge 1:1 village_name ward district using "${analysis}/Kilimanjaro google_ViltoCities.dta"
		drop _merge
		rename (district ward village_name) (survey_district survey_ward survey_village)
		
		gen survey_region="Kilimanjaro"
		
		tempfile kili_vilaccess
		save `kili_vilaccess'
		
	// Manyara
		use "${analysis}/Manyara ViltoAV_censusappended.dta", clear
		merge 1:1 village_id using "${analysis}/Manyara ViltoMaizemarket.dta"
		drop _merge
		merge 1:1 village_id using "${analysis}/Manyara ViltoCities_dist.dta"
		drop _merge
		
	append using `kili_vilaccess'
	
	fsum `market_access_varlist'
	
	// generate MARKET ACCESS PROXIES
		foreach var in city_km city_hrs nearcity_km{
			gen l_google_vil_`var' = log(google_vil_`var')
			egen std_google_vil_`var' = std(google_vil_`var')
		}	
		

		gen DH_access = .
		foreach city in moshi arusha babati dodoma tanga{
			replace DH_access = 0 if !mi(google_vil_`city'_km)
		}
		foreach city in moshi arusha babati dodoma tanga{
			gen tau_`city' = 2.7*(0.9392 + 0.02019*google_vil_`city'_km)/25
			replace DH_access = DH_access + (1 + tau_`city')^(${elasticity})*pop_frac_`city'
		}
		egen DH_access_std = std(DH_access)
		replace DH_access_std = -DH_access_std
	
	// regression between two remoteness measures
		reg std_google_vil_city_km DH_access_std, robust
		sum google_vil_city_km if e(sample)
		sum DH_access if e(sample)
		

	#d;
	tokenize `market_access_varlist';
	local vars=wordcount("`market_access_varlist'");
	di `vars';
	
	local i 1;
	local faocontrol "fao_yclr fao_ychr fao_sxlr fao_sxhr";
	while `i' <= `vars' {;
	
		local append_replace="append";
		if `i'==1 {; local append_replace="replace";};
		xi: reg ``i'' `indep', robust;
		sum ``i'' if e(sample);
		local mean=r(mean);
		local sd=r(sd);
		
		quietly outreg2 using "${pool_results}/PooledRegions Villagelevelaccess_`filename'.xls", nonote se symb(***,**,*) `append_replace' dec(2)
		 addstat("mean of depvar", `mean', "sd of depvar", `sd') keep(`indep');
	
		local i = `i' + 1;
	};
	#d cr
		
}


